package top.zerocn.main;

import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

import top.zerocn.dao.UserOperator;
import top.zerocn.entity.User;

public class App {

	private UserOperator userOperator = new UserOperator();
	private Scanner in = new Scanner(System.in);
	private PrintStream out = System.out;

	private User user;
	private boolean isLogin;

	public static void main(String[] args) {
		new App().mainMenu();
	}

	public void mainMenu() {
		out.println("|-------------------------------------------------------");
		out.println("|                   欢迎使用中国XXX行ATM                   ");
		out.println("|           1、登录");
		out.println("|           2、查询");
		out.println("|           3、注册");
		out.println("|           4、退出");
		out.println("|-------------------------------------------------------");
		out.print("请输入功能序列号：");

		do {
			int flg = 4;
			// 容错处理
			boolean repeat;
			do {
				try {
					flg = Integer.parseInt(in.next().trim());
					repeat = false;
				} catch (Exception e) {
					out.print("请输入正确的编号：");
					repeat = true;
				}
			} while (repeat);

			if (flg == 1) {
				if (isLogin) {
					out.println("已登录，请勿重复登录！！！");
				} else {
					login();
				}
			}
			if (flg == 2) {
				if (isLogin) {
					query();
				} else {
					out.println("请先登录！！！");
				}
			}
			if (flg == 3) {
				if (isLogin) {
					out.println("该账户登录中...已帮您下线");
					isLogin = false;
					user = null;
				}
				register();
			}
			if (flg == 4) {
				out.println("********** 退出系统 **********");
				out.println("感谢您的使用！再见");
				System.exit(0);
			}
			out.print("请输入对应指令编号：");
		} while (true);
	}

	public void login() {
		out.print("请输入您的账号：");
		String id = in.next().trim();
		user = userOperator.getUser(id);

		out.print("请输入您的密码：");
		String password = in.next().trim();
		int i = 0;
		for (; i < 2; i++) {
			if (Objects.nonNull(user) && Objects.equals(password, user.getPassword())) {
				isLogin = true;
				break;
			}
			out.print("用户名或密码不正确！\n请重新输入密码：");
			password = in.next().trim();
		}

		if (i == 2) {
			out.println("密码已输错3次，为了您的账户安全，请下次再试！");
			out.println("********** 退出系统 **********");
			System.exit(0);
		}

		loginMenu();
	}

	public void query() {
		out.println("|-------------------------------------------------------");
		out.println("|");
		out.println("|             正在处理，请稍后......");
		out.println("|");
		out.println("|-------------------------------------------------------");

		out.println("|-------------------------------------------------------");
		out.println("|    当前账户：" + user.getId());
		out.println("|-------------------------------------------------------");
		out.println("|    当前余额：" + user.getBalance() + "RBM");
		out.println("|-------------------------------------------------------");
		out.println("1、取款");
		out.println("2、存款");
		out.println("任意键返回");
		String flg = in.next().trim();

		if ("1".equals(flg)) {
			out.print("请输入取款金额：");
			BigDecimal money = null;
			boolean repeat = false;
			do {
				try {
					money = new BigDecimal(in.next().trim());
					repeat = false;
				} catch (Exception e) {
					out.print("请输入正确的金额：");
					repeat = true;
					continue;
				}
				if (user.getBalance().compareTo(money) < 0) {
					out.print("取款金额大于您的账户余额！\n请重新输入：");
					repeat = true;
				} else {
					break;
				}
			} while (repeat);

			user.setBalance(user.getBalance().subtract(money));
			if (userOperator.saveUser(user)) {
				out.println("取款成功");
				query();
			} else {
				// 回滚
				user.setBalance(user.getBalance().add(money));
			}
		} else if ("2".equals(flg)) {
			out.print("请输入存款金额：");
			BigDecimal money = null;
			boolean repeat = false;
			do {
				try {
					money = new BigDecimal(in.next().trim());
					repeat = false;
				} catch (Exception e) {
					out.print("请输入正确的金额：");
					repeat = true;
				}
			} while (repeat);
			user.setBalance(user.getBalance().add(money));
			if (userOperator.saveUser(user)) {
				out.println("存款成功");
				query();
			} else {
				// 回滚
				user.setBalance(user.getBalance().subtract(money));
			}
		}

	}

	public void register() {
		out.println("欢迎注册");
		out.print("姓名：");
		String name = in.next().trim();

		out.print("性别：");
		String sex = null;
		do {
			sex = in.next().trim();
			if ("男".equals(sex) || "女".equals(sex) || "保密".equals(sex)) {
				break;
			} else {
				out.print("性别的取值只能是男、女或保密！\n请重新输入");
			}
		} while (true);

		out.print("密码：");
		String password = in.next().trim();

		out.println("|-------------------------------------------------------");
		out.println("|");
		out.println("|             正在处理，请稍后......");
		out.println("|");
		out.println("|-------------------------------------------------------");

		String id = null;
		Random random = new Random();
		do {
			id = "";
			for (int i = 0; i < 12; i++) {
				id += random.nextInt(10);
			}
			if (Objects.isNull(userOperator.getUser(id))) {
				break;
			}

		} while (true);

		boolean flg = userOperator.saveUser(new User(id, name, password, sex, BigDecimal.ZERO));
		if (!flg) {
			out.println("注册失败！请重试");
			register();
		}

		out.println("|-------------------------------------------------------");
		out.println("|      恭喜您，注册成功！！！");
		out.println("|-------------------------------------------------------");
		out.println("|   您的银行卡账号为：" + id);
		out.println("|   ***请牢记以便下次登录时使用！！***");
		out.println("|1、返回");
		out.println("|-------------------------------------------------------");

		do {
			out.print("输入1返回：");
			if ("1".equals(in.next().trim())) {
				break;
			}
		} while (true);
	}

	public void loginMenu() {
		out.println("|-------------------------------------------------------");
		out.println("|                   欢迎使用中国XXX行ATM                   ");
		out.print("|                               " + user.getName().charAt(0));
		if ("男".equals(user.getSex())) {
			out.print("先生");
		} else if ("女".equals(user.getSex())) {
			out.print("女士");
		} else {
			out.print("先生（女士）");
		}
		out.println("您好！");
		out.println("|           2、查询");
		out.println("|           3、注册");
		out.println("|           4、退出");
		out.println("|-------------------------------------------------------");
		out.print("请输入功能序列号：");

		do {
			int flg = 4;
			// 容错处理
			boolean repeat;
			do {
				try {
					flg = Integer.parseInt(in.next().trim());
					repeat = false;
				} catch (Exception e) {
					out.print("请输入正确的编号：");
					repeat = true;
				}
			} while (repeat);

			if (flg == 2) {
				query();
			}
			if (flg == 3) {
				if (isLogin) {
					out.println("登录中...已帮您下线");
					isLogin = false;
					user = null;
				}
				register();
				mainMenu();
			}
			if (flg == 4) {
				out.println("********** 退出系统 **********");
				out.println("感谢您的使用！再见");
				System.exit(0);
			}
			out.print("请输入对应指令编号：");
		} while (true);

	}
}
